package com.minimalist.common.feign.fallback.basic;

import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.basic.vo.dict.DictCacheVO;
import com.minimalist.common.entity.basic.vo.dict.DictInfoVO;
import com.minimalist.common.entity.basic.vo.dict.DictQueryVO;
import com.minimalist.common.entity.basic.vo.dict.DictVO;
import com.minimalist.common.feign.client.basic.DictFeignClient;
import com.minimalist.common.feign.config.BaseFallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 字典管理 Feign 客户端降级工厂
 * 使用FallbackFactory可以获取到异常信息，便于问题排查
 *
 * @author minimalist
 */
@Component
public class DictFeignClientFallbackFactory extends BaseFallbackFactory<DictFeignClient>{
    private static final Logger log = LoggerFactory.getLogger(DictFeignClientFallbackFactory.class);


    @Override
    public DictFeignClient create(Throwable cause){
        return new DictFeignClient(){
            @Override
            public ResponseEntity<Void> addDict(DictInfoVO dictInfoVO){
                logServiceUnavailable("添加字典", dictInfoVO, cause);
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
            }

            @Override
            public ResponseEntity<Void> deleteDictByDictId(Long dictId){
                logServiceUnavailable("删除字典", dictId, cause);
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
            }

            @Override
            public ResponseEntity<Void> updateDictByDictId(DictInfoVO dictInfoVO){
                logServiceUnavailable("修改字典", dictInfoVO, cause);
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
            }

            @Override
            public ResponseEntity<PageResp<DictVO>> getPageDictList(DictQueryVO queryVO){
                logServiceUnavailable("分页查询字典列表", queryVO, cause);
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(new PageResp<>(List.of(), 0));
            }

            @Override
            public ResponseEntity<DictInfoVO> getDictByDictId(Long dictId){
                logServiceUnavailable("根据ID查询字典", dictId, cause);
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
            }

            @Override
            public ResponseEntity<List<DictCacheVO>> getDictByDictType(String dictType){
                logServiceUnavailable("根据字典类型查询字典", dictType, cause);
                return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).build();
            }
        };
    }
}
